<template>
  <div class="order-list">
    <!-- 搜索框 -->
    <el-input
      v-model="searchQuery"
      placeholder="搜索订单号、植物名称或用户"
      @input="searchOrders"
      class="search-input"
    />
    <el-table 
      :data="filteredOrders"
      border
    >
      <el-table-column 
        prop="id"
        label="订单号"
        />
      <el-table-column 
        prop="plantName"
        label="植物名称"
        />
      <el-table-column 
        prop="userName"
        label="用户"
        />
      <el-table-column 
        prop="status"
        label="状态"
      >
        <template #default="{ row }">
          <el-tag :type="getStatusTagType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="filteredOrders.length"
    />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

// 原始订单数据
const orders = ref([])
// 搜索关键词
const searchQuery = ref('')
// 当前页码
const currentPage = ref(1)
// 每页显示数量
const pageSize = ref(10)

// 模拟从服务器获取订单数据
onMounted(() => {
  orders.value = [
    { id: '20240501', plantName: '仙人掌', userName: '张三', status: '已发货' },
    { id: '20240502', plantName: '绿萝', userName: '李四', status: '待支付' }
  ]
})

// 过滤后的订单数据
const filteredOrders = computed(() => {
  return orders.value.filter(order => {
    const query = searchQuery.value.toLowerCase()
    return (
      order.id.toLowerCase().includes(query) ||
      order.plantName.toLowerCase().includes(query) ||
      order.userName.toLowerCase().includes(query)
    )
  })
})

// 根据订单状态获取对应的标签类型
function getStatusTagType(status) {
  switch (status) {
    case '已发货':
      return 'success'
    case '待支付':
      return 'warning'
    case '已取消':
      return 'info'
    default:
      return ''
  }
}

// 获取订单状态文本
function getStatusText(status) {
  return status
}

// 搜索订单
function searchOrders() {
  currentPage.value = 1
}

// 每页数量变化时触发
function handleSizeChange(val) {
  pageSize.value = val
}

// 当前页码变化时触发
function handleCurrentChange(val) {
  currentPage.value = val
}
</script>

<style scoped>
.order-list {
  padding: 20px;
}

.search-input {
  width: 200px;
  margin-bottom: 20px;
}
</style>
